//
//  SelectReceiverList.m
//  OASystem
//
//  Created by macos on 14-4-18.
//  Copyright (c) 2014年 macos. All rights reserved.
//

#import "SelectReceiverList.h"

@interface SelectReceiverList ()

@end

@implementation SelectReceiverList

@synthesize selectListArr;
@synthesize allReceiverListArr;
@synthesize delegate;
@synthesize flag;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        if(!selectListArr)
            selectListArr = [[NSMutableArray alloc] init];
        
        if(!allReceiverListArr)
            allReceiverListArr = [[NSArray alloc] init];
    }
    return self;
}

//返回上一级
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

//选择收件人确认+返回
- (void)commit
{
    [self.navigationController popViewControllerAnimated:YES];
    [delegate passList:selectListArr];
}

- (void)initNavi
{
    //初始化UINavigation
    self.navigationItem.titleView = [[UIView alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
    UILabel *titleLabel           = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabel.text               = @"选择收件人";
    titleLabel.font               = HEIGHT_FONT(18.0f);
    titleLabel.textAlignment      = NSTextAlignmentCenter;
    titleLabel.backgroundColor    = [UIColor clearColor];
    titleLabel.textColor          = [UIColor whiteColor];
    [self.navigationItem.titleView addSubview:titleLabel];
    
    //添加导航又边用户信息
    //初始化UINavigation左按钮
    UIButton *naviRightButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 45, 24)];
    [naviRightButton setTitle:@"完成" forState:UIControlStateNormal];
    naviRightButton.titleLabel.font = HEIGHT_FONT(13.0f);
    [naviRightButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_n.png"] forState:UIControlStateNormal];
    [naviRightButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_p.png"] forState:UIControlStateHighlighted];
    [naviRightButton addTarget:self action:@selector(commit) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:naviRightButton];
    
    //初始化UINavigation左按钮
    UIButton *naviButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 45, 24)];
    [naviButton setTitle:@"返回" forState:UIControlStateNormal];
    naviButton.titleLabel.font = HEIGHT_FONT(13.0f);
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_n.png"] forState:UIControlStateNormal];
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_p.png"] forState:UIControlStateHighlighted];
    [naviButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:naviButton];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self initNavi];
    selectReceiverTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    if(!departMentDic)
        departMentDic = [[NSMutableDictionary alloc] init];
    if(!departArr)
        departArr = [[NSMutableArray alloc] init];
    
    NSString *temp;
    if(flag == 0)
    {
        NSLog(@"selectListArr--->%@",selectListArr);
        if(selectListArr.count != 0)
            temp = [selectListArr objectAtIndex:0];
        else
            temp = @"";
    }
    
    
    NSMutableArray *tempKeyArr = [[NSMutableArray alloc] init];
    for (int i = 0; i<allReceiverListArr.count; i++)
    {
        if(flag == 0)
        {
            if(selectListArr.count != 0)
            {
                if([temp isEqualToString:[[allReceiverListArr objectAtIndex:i] objectAtIndex:0]])
                {
                    [selectListArr removeAllObjects];
                    [selectListArr addObject:allReceiverListArr[i]];
                }
            }
        }
    
        if(![[departMentDic allKeys] containsObject:[[allReceiverListArr objectAtIndex:i] objectAtIndex:3]])
        {
            NSLog(@"kay--------->%@",[[allReceiverListArr objectAtIndex:i] objectAtIndex:3]);
            [tempKeyArr addObject:[[allReceiverListArr objectAtIndex:i] objectAtIndex:3]];
            [departMentDic setObject:@[[allReceiverListArr objectAtIndex:i]] forKey:[[allReceiverListArr objectAtIndex:i] objectAtIndex:3]];
            [departArr addObject:[[allReceiverListArr objectAtIndex:i] objectAtIndex:3]];
        }
        else
        {
            NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:[departMentDic objectForKey:[[allReceiverListArr objectAtIndex:i] objectAtIndex:3]]];
            [temp addObject:[allReceiverListArr objectAtIndex:i]];
            [departMentDic setObject:temp forKey:[[allReceiverListArr objectAtIndex:i] objectAtIndex:3]];
        }
    }
    
    NSLog(@"kayall--------->%@",tempKeyArr);
    
    if(!displayListArr)
        displayListArr = [[NSArray alloc] init];
    
    for (int i = 0; i<tempKeyArr.count; i++)
    {
        NSLog(@"kay--all--------->%@",tempKeyArr[i]);
        NSLog(@"kay-2-all--------->%@",[[departMentDic allKeys] objectAtIndex:i]);
        displayListArr = [displayListArr arrayByAddingObjectsFromArray:@[tempKeyArr[i]]];
        displayListArr = [displayListArr arrayByAddingObjectsFromArray:[departMentDic objectForKey:tempKeyArr[i]]];
    }
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if([departArr containsObject:[displayListArr objectAtIndex:indexPath.row]])
//        return 50;
//    else
        return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return displayListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
    for (UIView *subview in subviews)
    {
        [subview removeFromSuperview];
    }
    
    
    //自定义分割线
    UIImageView *line    = [[UIImageView alloc] initWithFrame:CGRectMake(0, 39, 320, 1)];
    line.backgroundColor = NOTICEBOARD_LINE_COLOR;
    [cell.contentView addSubview:line];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 150, 20)];
    if(![departArr containsObject:[displayListArr objectAtIndex:indexPath.row]])
        label.text     = [[displayListArr objectAtIndex:indexPath.row] objectAtIndex:4];
    else
        label.text     = [displayListArr objectAtIndex:indexPath.row];
    label.font     = HEIGHT_FONT(14.0f);
    [cell.contentView addSubview:label];
    
    if(![departArr containsObject:[displayListArr objectAtIndex:indexPath.row]])
    {
        //是否选中
        UIImageView *selectFlagImg = [[UIImageView alloc] initWithFrame:CGRectMake(290, 12, 16, 16)];
        if([selectListArr containsObject:[displayListArr objectAtIndex:indexPath.row]])
            selectFlagImg.image = [UIImage imageNamed:@"contact_checkbox_c.png"];
        else
            selectFlagImg.image = [UIImage imageNamed:@"contact_checkbox_n.png"];
        [cell.contentView addSubview:selectFlagImg];
    }
    else
    {
        if(line)
            [line removeFromSuperview];
        
        UIImageView *selectFlagImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 39, 320, 2)];
        selectFlagImg.backgroundColor = [UIColor colorWithRed:60/255.0 green:107/255.0 blue:199/255.0 alpha:1.0f];
        [cell.contentView addSubview:selectFlagImg];
        
    }
    
    cell.contentView.backgroundColor = NOTICEBOARD_BACKCOLOR;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!selectListArr)
        selectListArr = [[NSMutableArray alloc] init];
    
    if(![departArr containsObject:[displayListArr objectAtIndex:indexPath.row]])
    {
        if([selectListArr containsObject:[displayListArr objectAtIndex:indexPath.row]])
            [selectListArr removeObject:[displayListArr objectAtIndex:indexPath.row]];
        else
            [selectListArr addObject:[displayListArr objectAtIndex:indexPath.row]];
        
        [selectReceiverTable reloadData];
    }
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
